/*
 * Copyright 2008 Google Inc. All Rights Reserved.
 * Author: fraser@google.com (Neil Fraser)
 * Author: anteru@developer.shelter13.net (Matthaeus G. Chajdas)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Difference Match and Patch
 * http://code.google.com/p/google-diff-match-patch/
 */
/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 **/
using System;

namespace Peter.Logic.DiffEngine
{
   /// <summary>
   /// Represents a difference operation.
   /// </summary>
   public class Difference
   {
      /// <summary>
      /// Constructor. Initializes the diff with the provided values.
      /// </summary>
      /// <param name="operation">One of INSERT, DELETE or EQUAL.</param>
      /// <param name="text">The text being applied.</param>
      public Difference (Operation operation, string text)
      {
         // Construct a diff with the specified operation and text.
         this.Operation = operation;
         this.Text = text;
      }

      /// <summary>
      /// Gets or Sets the operation.
      /// </summary>
      public Operation Operation { get; set; }

      /// <summary>
      /// Gets or Sets the text associated with this diff operation.
      /// </summary>
      public string Text { get; set; }

      /// <summary>
      /// Returns a string that represents the current object.
      /// </summary>
      /// <returns>
      /// A string that represents the current object.
      /// </returns>
      /// <filterpriority>2</filterpriority>
      public override string ToString ()
      {
         string prettyText = this.Text.Replace ('\n', '\u00b6');
         return "Difference(" + this.Operation + ",\"" + prettyText + "\")";
      }

      /// <summary>
      /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
      /// </summary>
      /// <returns>
      /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
      /// </returns>
      /// <param name="obj">The object to compare with the current object. </param><filterpriority>2</filterpriority>
      public override bool Equals (Object obj)
      {
         // If parameter is null return false.
         if (obj == null)
            return false;

         // If parameter cannot be cast to Difference return false.
         var p = obj as Difference;
         if (p == null)
            return false;

         // Return true if the fields match.
         return p.Operation == this.Operation && p.Text == this.Text;
      }

      /// <summary>
      /// Is this Difference equivalent to another Difference?
      /// </summary>
      /// <param name="difference">The difference to compare this difference to.</param>
      /// <returns>True if differences are equal, otherwise false.</returns>
      public bool Equals (Difference difference)
      {
         // If parameter is null return false.
         if (difference == null)
            return false;

         // Return true if the fields match.
         return difference.Operation == this.Operation && difference.Text == this.Text;
      }

      /// <summary>
      /// Serves as a hash function for a particular type. 
      /// </summary>
      /// <returns>
      /// A hash code for the current <see cref="T:System.Object"/>.
      /// </returns>
      /// <filterpriority>2</filterpriority>
      public override int GetHashCode ()
      {
         return Text.GetHashCode () ^ Operation.GetHashCode ();
      }
   }
}